<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Repositories\ControlList;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Dcat\Admin\Support\JavaScript;

class ControlListController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ControlList(['user']), function (Grid $grid) {
            $grid->quickSearch('user.telephone')->placeholder('模糊搜索电话');
            $grid->column('id')->sortable();
            $grid->column('user.name', '用户名');
            $grid->column('user.flag', '用户标签');
            $grid->column('user.telephone', '会员电话')->copyable();
            $grid->column('control');
            $grid->column('status', '状态')->using([
                0 => '正常',
                1 => '必中',
                2 => '必不中',
            ]);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('user.name', '用户名');
                $filter->like('user.telephone', '用户电话');
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ControlList(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('control');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ControlList(), function (Form $form) {
            $js = <<<'JS'
    function (res) {
    console.info(res.data)
    let data = res.data;
    str = "姓名:"+ data.name+"  手机号:"+data.telephone+"   id:"+data.id;
    Dcat.swal.success("确定用户",str);
    }
JS;
            $form->display('id');

            $form->autocomplete('user_id', '用户id')
                ->configs([
                    'onSelect' => JavaScript::make($js),
                ])
                ->ajax('/api/get_user_id', 'id', 'telephone')
                ->help('输入手机号自动查询')
                ->required();

            $form->select('status', '状态')->options([
                0 => '正常',
                1 => '必中',
                2 => '必不中',
            ])->default(0);
            $form->number('control')->help('如果要使用强制中单数，请把状态调整正常！状态优先级会覆盖此优先级')->required();
            $form->saving(function ($form) {
                if (! $form->isEditing()) {
                    if (\App\Models\ControlList::query()->where('user_id', $form->user_id)
                        ->first()
                    ) {
                        return $form->response()->error('该用户已在名单');
                    }
                }
            });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
